<?php 
/**
 * 
 * @see 04/Abril/2012
 * @author Rosanna Paberli Jimenez Rosario
 *
 */
class DocumentosEstatusPagina extends Controller
{
	
	public function __construct()
	{
		
		parent::Controller();
		$this->load->model(array("documentosestatuspaginamodel", "documentosestatusmodel"));
		$this->load->library('pagination');
	}

	
    public function index()
	{
		$this->load->view('main',array('content_page' => 'documentosestatuspagina_list', 'menutop' => 0));	
	}
	

	public function data()
	{
		
		 $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		 $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'id_documento'; // Columna para el order by
		 $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		 $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;

		 $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );

	    $records = $this->documentosestatusmodel->loadRegistros($start, $limit, $sortname, $sortorder);

		$totalRecords = count($records);
		
		 $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		       $page = $totalPages;

		    $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false){
			     foreach($records as $objetos){
			     		
			         $cantidad = 0;
			         
			     	 $cantidad = $this->documentosestatuspaginamodel->loadRegistrosByIdDocumento($objetos->getId_documento());
			     	 
				     $arr = $json_output['rows'][] = array(
				          'id_documento'           => $objetos->getId_documento(),
				     	  'iddoc'                  => $objetos->getId_documento(),
				          'numero_pagina'          => count($cantidad),
				     	  'titulo_documento'       => $objetos->getTitulo_documento()
				      );
			      }
		      }

		 echo json_encode($json_output);
	}
	
	
	public function detalleDocumento()
	{

		
		 $page = $this->input->post('page') ? $this->input->post('page') : 50; // Pagina Actual o 1 por default
		 $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'numero_pagina'; // Columna para el order by
		 $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc'; // Orden (asc|desc)
		 $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;

		 $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );
		  
	      $records = $this->documentosestatuspaginamodel->loadRegistrosByIdDocumento($this->input->post('iddetalle'), $start, $limit, $sortname, $sortorder);
	      
		  $totalRecords = count($records);
		
		  $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		       $page = $totalPages;

		    $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false){
			     foreach($records as $objetos){

				     $arr = $json_output['rows'][] = array(
				          'id_pagina'              => $objetos->getId_pagina(),
				     	  'id_documento'           => $objetos->getId_documento(),
				          'numero_pagina'          => $objetos->getNumero_pagina(),
				     	  'contenido'              => "Contenido"
				      );
			      }
		      }
		      
		 echo json_encode($json_output);
	
	}
	
	
	public function add($id)
	{
		
		$tituloDocumento = $id;

		$numeroPagina  = $this->documentosestatuspaginamodel->loadRegistrosByIdDocumento($id);
		
		$cantidad = array();
		
		$proximo = 1;
	
		if($numeroPagina == ""){
			
			$cantidad[1] = 1;
			
		}else{
			
			
		foreach ($numeroPagina as $numeros){
			
		   $cantidad[$numeros->getNumero_pagina()] = $numeros->getNumero_pagina();

		}
		
		if(count($cantidad) >= 1){
			
			$proximo = count($cantidad) + 1;
			$cantidad[] = $proximo;
			
		}else{
			
			$cantidad[1] = 1;
		}
		      
	}
		$this->load->view('third_view', array('content_page' => 'documentosestatuspagina_add',
										  	  'menutop'      => 0,
									          'tituloDoc'    => $tituloDocumento,
										      'numero'       => $proximo));
   }
	
	public function edit($idDocumentoPagina)
	{
		
		
		
		    $idPagina = explode("-",$idDocumentoPagina);

		    $registros = $this->documentosestatuspaginamodel->loadRegistrosById($idPagina[0]);

		    $this->load->view('third_view', array('content_page' => 'documentosestatuspagina_edit',
								        	'menutop'      => 0,
											'registros'    => $registros,
								        	'tituloDoc'    => $idPagina[1]));
		   
		
	}
	
	
	public function delete()
	{
		$id = $this->input->post('delete_rows');
   		$this->documentosestatuspaginamodel->eliminar($id);
	}
	
	
	public function save()
	{
		
		
	   $data = array("numero_pagina"      => $this->input->post('numero_pagina'),
	   				 "contenido_pagina"   => htmlentities($this->input->post('contenido')),
	   				 "id_documento"       => $this->input->post('tipo_documento'));  
					
   	
   	   if ($this->input->post('id_pagina') == false) {
   	   	   if ($this->documentosestatuspaginamodel->agregar($data) == true) {
   	   	   	   $mensaje = "El resgistro se agrego exitosamente.";
   	   	   	   $router = true;
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	       $router = false;
   	   	   }
   	   	  
   	   }else{
   	       if ($this->documentosestatuspaginamodel->editar($data, $this->input->post('id_pagina')) == true) {
   	   	   	   $mensaje = "El resgistro se edito exitosamente";
   	   	   	   $router = true;
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	       $router = false;
   	   	   }
   	   }
   	   
   	   $this->session->set_flashdata('error_title',  $mensaje);
   	   $this->session->set_flashdata('error_router', $router);
   	   redirect('documentosestatuspagina');
   	   
	}
	
	
}
